const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const config = require('./src/config/config')

module.exports = {
    entry: {
        main: './src/main',
        vendors: './src/vendors'
    },
    output: {
        path: path.join(__dirname, './dist'),
        filename: '[name].js',
        chunkFilename: 'chunk/[name].[hash:8].chunk.js'
    },
    resolve: {
        extensions: ['.js', '.vue', '.json','svg','png','jpg'],
        alias: {
            '@': path.resolve(__dirname, './src')
        }
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: [
                    {
                        loader: 'vue-loader',
                        options: {
                            loaders: {
                                // less: ExtractTextPlugin.extract({
                                //     use: ['css-loader?minimize', 'autoprefixer-loader', 'less-loader'],
                                //     fallback: 'vue-style-loader'
                                // }),
                                css: ExtractTextPlugin.extract({
                                    use: ['css-loader', 'autoprefixer-loader', 'less-loader'],
                                    fallback: 'vue-style-loader'
                                })
                            }
                        }
                    },
                    {
                        loader: 'iview-loader',
                        options: {
                            prefix: false
                        }
                    }
                ]
            },
            {
                test: /iview\/.*?js$/,
                loader: 'babel-loader'
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    use: ['css-loader?minimize', 'autoprefixer-loader'],
                    fallback: 'style-loader'
                })
            },
            {
                test: /\.less/,
                use: ExtractTextPlugin.extract({
                    use: ['css-loader?minimize', 'autoprefixer-loader', 'less-loader'],
                    fallback: 'style-loader'
                })
            },
            {
                test: /\.(jpg|png|gif|svg|ico)$/,
                loader: 'url-loader',
                options: {
                    esModule: false,
                    limit: 8*1024,
                    outputPath: 'images/',
                    name:'images/[name]-[hash:8].[ext]'
                }
            },
            {
                test: /\.(woff|woff2|ttf|eot)$/,
                loader: 'url-loader',
            },
            {
                test: /\.(html|http|rest|tpl)$/,
                loader: 'html-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: config.app_name,
            inject: 'body',
            hash: true,
            template: path.resolve(__dirname, "./src/template/index.html"),
            favicon: path.resolve(__dirname, "./src/static/iview-favicon.ico"),
            minify: {
                html5: true,
                collapseWhitespace: true,
                preserveLineBreaks: false,
                minifyCSS: true,
                minifyJS: true,
                removeComments: true
            }
        }),
        new webpack.DefinePlugin({
            base_config: `${JSON.stringify(config)}`
        })
    ]
};